import login from '../../api/login'

// initial state
// shape: [{ id, quantity }]
const state = () => ({
    username: '',
    password: '',
    token: '',
})

// getters
const getters = {
    // console.log("token", this.$store.getters['user/token'])
    token: (state, getters, rootState) => {
        return state.token;
    },

}

// actions
const actions = {
    // this.$store.dispatch("user/Login", this.loginForm)
    Login({commit, state}, loginForm) {
        return new Promise(((resolve, reject) => {
            login.handleLogin(loginForm,
                (response, token) => {
                    commit('setToken', token);
                    resolve(response);
                },
                () => {
                    commit('setToken', '');
                    reject();
                    // commit('')
                })

        }))
    },
    register({commit, state}, loginForm) {
        return new Promise(((resolve, reject) => {
            login.handleRegister(loginForm,
                (response) => {
                    resolve(response);
                },
                () => {
                    reject();
                })

        }))
    },
}

// mutations
const mutations = {
    setToken(state, token) {
        state.token = token;
    },

}

export default {
    namespaced: true,
    state,
    getters,
    actions,
    mutations
}